package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.MessageDao;
import dao.RorderDao;
import model.Message;
import model.Rorder;

/**
 * Servlet implementation class RorderStatusServlet
 */
@WebServlet("/RorderStatusServlet")
public class RorderStatusServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RorderStatusServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	//	response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String handle = request.getParameter("handle");
		System.out.println(handle);
		if(handle !=null && handle.equals("1Status")) {
			String rname = request.getParameter("rname");
			String temail = request.getParameter("temail");
			System.out.println(rname+" "+temail);
			try {
				Rorder rorder = RorderDao.newRorder(temail, rname);
				if(RorderDao.getRorderNoCacheForBool(rorder)) {
					rorder.setStatus(1);
					RorderDao.updateRorder(rorder);
					System.out.println("11");
					if(RorderDao.getRorderNoCache(rorder).getStatus()==1) {
						String flag = "1";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminOrderList2.jsp").forward(request, response);
						
					} else {
						String flag = "0";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminOrderList2.jsp").forward(request, response);
					}
				} else {
					String flag = "0";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("/admin/AdminOrderList2.jsp").forward(request, response);
				}
			} catch (Exception e) {
				e.printStackTrace();
				String flag = "0";
				request.setAttribute("flag", flag);
				request.getRequestDispatcher("/admin/AdminOrderList2.jsp").forward(request, response);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		if(handle !=null && handle.equals("0Status")) {
			String rname = request.getParameter("rname");
			String temail = request.getParameter("temail");
			try {
				Rorder rorder = RorderDao.newRorder(temail, rname);
				if(RorderDao.getRorderNoCacheForBool(rorder)) {
					rorder.setStatus(0);
					RorderDao.updateRorder(rorder);
					System.out.println("11");
					if(RorderDao.getRorderNoCache(rorder).getStatus()==1) {
						String flag = "1";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminOrderList2.jsp").forward(request, response);
						
					} else {
						String flag = "0";
						request.setAttribute("flag", flag);
						request.getRequestDispatcher("/admin/AdminOrderList2.jsp").forward(request, response);
					}
				} else {
					String flag = "0";
					request.setAttribute("flag", flag);
					request.getRequestDispatcher("/admin/AdminOrderList2.jsp").forward(request, response);
				}
			} catch (Exception e) {
				e.printStackTrace();
				String flag = "0";
				request.setAttribute("flag", flag);
				request.getRequestDispatcher("/admin/AdminOrderList2.jsp").forward(request, response);
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
